#include    "picture.h"
#include    <stdio.h>
#include    <string.h>
#include    "../texture/texture2d.h"


Picture::Picture(unsigned int numberOfTextures)
{
	m_textures = new GLuint[numberOfTextures];
	m_textureIndex = 0;
    m_currentFrame = 0;
    m_numberOfFrames = 0;
    m_size.width = 0;
    m_size.height= 0;
    m_useTransparency = false;
    m_isSpriteSheet = false;
}

Picture::~Picture()
{
	delete[] m_textures;
}

bool Picture::addTexture(const char* fileName,bool useTransparency)
{

	GLuint texture = PersonalTexture::Texture2D::LoadTexture(fileName);

    m_textures[m_textureIndex] = texture;
    m_textureIndex++;
    if(m_textureIndex == 1 && m_numberOfFrames > 1)
    {
        m_isSpriteSheet = true;
    }else
    {
        m_isSpriteSheet = false;
    }

    m_useTransparency   =   useTransparency;
    return true;
}

void Picture::render(GLfloat x,GLfloat y)
{
	if(m_useTransparency)
    {
        glEnable(GL_BLEND);
        glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
    }

    glBindTexture(GL_TEXTURE_2D,getCurrentFrame());
    glBegin(GL_QUADS);
    GLfloat w = m_size.width;
    GLfloat h = m_size.height;
    GLfloat texWidth = (GLfloat)m_textureIndex/(GLfloat)m_numberOfFrames;
    GLfloat texHeigth = 1.0f;
    GLfloat u = 0.0f;
    GLfloat v = 0.0f;

    if(m_textureIndex < m_numberOfFrames)
    {
        u = (GLfloat)m_currentFrame*texWidth;
    }

    glTexCoord2f(u,v);glVertex2f(x,y);
    glTexCoord2f(u + texWidth,v );glVertex2f(x + w,y);
    glTexCoord2f(u + texWidth,v + texHeigth);glVertex2f(x + w,y + h);
    glTexCoord2f(u , v + texHeigth);glVertex2f(x , y + h);
    glEnd();

    if(m_useTransparency)
    {
        glDisable(GL_BLEND);
    }

}




//new picture.h

SinglePicture::SinglePicture()
{
    m_texture = 0;
}

bool SinglePicture::loadTexture(const char* fileName)
{
    if(m_texture != 0)
    {
        printf("picture already load,can't load [%s] any more!\n",fileName);
        return false;
    }

    m_texture = PersonalTexture::Texture2D::LoadTexture(fileName);
    return m_texture != 0;
}


MultiPicture::MultiPicture(unsigned int numberOfPictures)
{
    m_numberOfPictures = numberOfPictures;
    m_textures = new GLuint[m_numberOfPictures];
    memset(m_textures,0,sizeof(GLuint)*m_numberOfPictures);
    m_textureIndex = 0;
}

MultiPicture::~MultiPicture()
{
    delete[] m_textures;
    m_textures = 0;
}

GLuint MultiPicture::getTexture(unsigned int index)const
{
    if(index >= m_textureIndex)
    {
        printf("index:[%d] big than picture max index\n",index);
        return 0;
    }

    return m_textures[index];
}

bool MultiPicture::loadTexture(const char* fileName)
{
    if(m_textureIndex == m_numberOfPictures)
    {
        printf("pictures load full,can't load <%s> \n",fileName);
        return false;
    }

    GLuint texture = PersonalTexture::Texture2D::LoadTexture(fileName);

    if(texture == 0)
    {
        return false;
    }

    m_textures[m_textureIndex] = texture;
    m_textureIndex++;
    return true;
}

